/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_MSDP_SPATIALAWARENESS_NAPI_H
#define OHOS_MSDP_SPATIALAWARENESS_NAPI_H

#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "nlohmann/json.hpp"
#include "application_env.h"

#include "spatial_napi_string.h"
#include "spatial_native_callback.h"
#include "spatial_native_event.h"
#include "spatial_device_info.h"
#include "spatial_location_mode.h"
#include "spatial_awareness_mgr_client.h"

namespace OHOS {
namespace Msdp {
class SpatialNapi : public SpatialNativeEvent {
public:
    explicit SpatialNapi(napi_env env, napi_value thisVar);
    virtual ~SpatialNapi();

    static napi_value Init(napi_env env, napi_value exports);
    static napi_value ModeInit(napi_env env, napi_value exports);
    static napi_value Constructor(napi_env env, napi_callback_info info);
    static napi_value SubscriptionSpatialRelationship(napi_env env, napi_callback_info info);
    static napi_value UnSubscriptionSpatialRelationship(napi_env env, napi_callback_info info);
    static napi_value EnumSpatialModeConstructor(napi_env env, napi_callback_info info);
    static SpatialNapi *GetSpatialNapi();
    static napi_value CreateCallback(napi_env env, const std::string &eventMode, const SpatialDeviceInfo &deviceInfo);
    static void CreateSpatialCallback(napi_env env,
        const SpatialLocationMode &spatialMode, const SpatialDeviceInfo &info);
    static void ReleaseSpatialCallback(const SpatialLocationMode &spatialMode, const SpatialDeviceInfo &info);
    static napi_value JsToSpatialMode(napi_env env, napi_value exports);
    static void JsToSpatialDeviceInfo(const napi_env &env, const napi_value &object,
        SpatialDeviceInfo &info);
    static void JsObjectToString(const napi_env &env, const napi_value &object,
        const std::string &fieldStr, char *dest, const int32_t destLen);
    static std::string JsObjectToString(const napi_env &env, const napi_value &object);
    static void JsObjectToInt(const napi_env &env, const napi_value &object,
        const std::string &fieldStr, int& fieldRef);
    static void CheckJsNapiType(const napi_env &env, const napi_valuetype &jsValueType,
        napi_value jsProValue, nlohmann::json &jsonObj, std::string strProName);
    static void JsToJsonObject(const napi_env &env, const napi_value &object,
        const std::string &fieldStr, nlohmann::json &jsonObj);
    static void JsToDmBuffer(const napi_env &env, const napi_value &object, const std::string &fieldStr,
        uint8_t **bufferPtr, int32_t &bufferLen);
    static void JsObjectToBool(const napi_env &env, const napi_value &object,
        const std::string &fieldStr, bool& fieldRef);
    static void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
        napi_value &result);
    static void SetValueUtf8String(const napi_env &env, const std::string &fieldStr, const std::string &str,
        napi_value &result);
    static void SetValueDouble(const napi_env &env, const std::string &fieldStr, const double &doubleValue,
    napi_value &result);
    static void SetNamedPropertyByStr(napi_env env,
        napi_value dstObj, const std::string &objName, const char *propName);
    void OnDirectionChangedDone(const DirectionResponse &directionRes);
    void OnDistanceChangedDone(const DistanceResponse &distanceRes);
    void OnAngleChangedDone(const AngleResponse &angleRes);
    void OnInnerOuterChangedDone(const InnerOuterResponse &ioRes);
    void OnExistChangedDone(const ExistResponse &existRes);
    static void Mapping();
private:
    napi_env env_;
    napi_ref wrapper_;
    static std::string bundleName_;
    static sptr<ISpatialLocationCallback> callback_;
    static thread_local napi_ref sConstructor_;
};
} // namespace Msdp
} // namespace OHOS
#endif // OHOS_MSDP_SPATIALAWARENESS_NAPI_H